using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace HZY.Admin.Services
{
    using System.Threading.Tasks;
    using HZY.EFCore.Repository;
    using HZY.Models;
    using HZY.Models.Sys;
    using HZY.Toolkits;
    using HZY.EFCore.Base;
    using HZY.Admin.Services.Core;
    using HZY.EFCore;
    using Microsoft.EntityFrameworkCore;

    /// <summary>
    /// 订单
    /// </summary>

    public class SalesSlipService : ServiceBase<SalesSlip>
    {
        private readonly DefaultRepository<Customer> customerDb;
        private readonly DefaultRepository<SalesSlipInfo_Items> salesslipinfo_itemsDb;
        private readonly DefaultRepository<Product> productDb;
        public SalesSlipService(EFCoreContext _db,DefaultRepository<Customer> customerDb , DefaultRepository<SalesSlipInfo_Items> salesslipinfo_itemsDb,DefaultRepository<Product> productDb) 
            : base(_db)
        {
            this.customerDb = customerDb;
            this.salesslipinfo_itemsDb = salesslipinfo_itemsDb;
            this.productDb = productDb;
        }

        #region CURD 基础

        /// <summary>
        /// 获取数据源
        /// </summary>
        /// <param name="Page"></param>
        /// <param name="Rows"></param>
        /// <param name="Search"></param>
        /// <returns></returns>
        public async Task<TableViewModel> FindListAsync(int Page, int Rows, SalesSlip Search)
        {
            var query = (
                from salesslip in this.db.SalesSlips
                from customer in this.db.Customers.Where(v => v.Customer_ID == salesslip.SalesSlip_CustomerId)
                
                select new { salesslip, customer  }
                )
                 .WhereIF(w => w.salesslip.SalesSlip_Name.Contains(Search.SalesSlip_Name), !string.IsNullOrWhiteSpace(Search?.SalesSlip_Name))
                 .OrderBy(w => w.salesslip.SalesSlip_Code)
                .Select(w => new
                {
                   
                    w.salesslip.SalesSlip_Name,
                    w.salesslip.SalesSlip_Code,
                    w.customer.Customer_Code,
                    w.customer.Customer_Name,
                    w.salesslip.SalesSlip_EntryTime,
                    w.salesslip.SalesSlip_CreateTime,
                    w.salesslip.SalesSlip_Handler,
                    w.salesslip.SalesSlip_IsComplete,
                    w.salesslip.SalesSlip_Remarks,
                    _ukid = w.salesslip.SalesSlip_ID
                });

            return await this.db.AsTableViewModelAsync(query, Page, Rows, typeof(SalesSlip), typeof(Customer) );
        }

        /// <summary>
        /// 新增\修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<Guid> SaveAsync(SalesSlip model)
        {
            await this.InsertOrUpdateAsync(model);

            return model.SalesSlip_ID;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="Ids"></param>
        /// <returns></returns>
        public async Task<int> DeleteAsync(List<Guid> Ids)
        => await this.DeleteAsync(w => Ids.Contains(w.SalesSlip_ID));

        /// <summary>
        /// 加载表单 数据
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<Dictionary<string, object>> LoadFormAsync(Guid Id)
        {
            var res = new Dictionary<string, object>();

            var Model = await this.FindByIdAsync(Id);
            var customerInfo = await customerDb.Query().OrderBy(v => v.Customer_Code).ToListAsync();
            res[nameof(Id)] = Id;
            res[nameof(Model)] = Model.NullSafe();
            res[nameof(customerInfo)] = customerInfo.NullSafe();
            return res;
        }
        /// <summary>
        /// 
        /// </summary>
        private List<Guid> Ids = new List<Guid>();
        /// <summary>
        /// 查询订单明细
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>

        public async Task<Dictionary<string, object>> GetSalesTimes(Guid Id)
        {
            var  salesitmesList = await (
                from productlist in   this.db.Products
                from salesitmessList    in this.db.SalesSlipInfo_Itemss.Where(v => v.SalesSlipInfo_Items_ProdectId == productlist.Product_ID)
                select new { productlist, salesitmessList }
                ).Where(w => w.salesitmessList.Master_Id == Id).
                OrderBy(o => o.productlist.Product_Code)
                .Select(s => new
                {
                    s.productlist.Product_Code,
                    s.productlist.Product_Name,
                    s.productlist.Product_Specs,
                    s.productlist.Product_Colour,
                    
                    s.salesitmessList.SalesSlipInfo_Items_Enclosure,
                    s.salesitmessList.SalesSlipInfo_Items_ProdectUnit,
                    s.salesitmessList.SalesSlipInfo_Items_ProdecUnitPrice,
                    s.salesitmessList.SalesSlipInfo_Items_Width,
                    s.salesitmessList.SalesSlipInfo_Items_Height,
                    s.salesitmessList.SalesSlipInfo_Items_ProdectNumber,
                    s.salesitmessList.SalesSlipInfo_Items_ProdecMoney,
                    _ukid = s.salesitmessList.SalesSlipInfo_Items_ID
                }).ToListAsync();  

            var res = new Dictionary<string, object>();
            res[nameof(salesitmesList)] = salesitmesList;
            return  res;

          //  return await this.db.AsTableViewModelAsync(query, Page, Rows, typeof(SalesSlip), typeof(Customer));


        }
        
        #endregion

        #region 导出 Excel

        /// <summary>
        /// 导出Excel
        /// </summary>
        /// <param name="Search"></param>
        /// <returns></returns>
        public async Task<byte[]> ExportExcel(SalesSlip Search)
        {
            var tableViewModel = await this.FindListAsync(1, 999999, Search);
            return this.ExportExcelByTableViewModel(tableViewModel);
        }

        #endregion

    }
}
